<?php
ob_start();
session_start();
include("../models/webconfig.php");
include("../models/class.connection.php");
include ("../models/entities/class.AccountEntity.php");
include ("../models/repositories/class.AccountRepository.php");
if (isset($_GET["logout"])) {
    unset ($_SESSION["Username"]);
    setcookie("UserName","",time()-60);
    setcookie("Password","",time()-60);
    unset ($_COOKIE["UserName"]);
    unset ($_COOKIE["Password"]);

}
if (isset($_POST["login_Save"])) {
    setcookie("UserName", $_POST["login_UserName"]);
    setcookie("Password", $_POST["login_Password"]);
}
$accountRes = new AccountRepository();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>|Login|</title>
    </head>
    <body bgcolor="#f6f6f6">
        <center>
            <table border="1px" >
                <tr style="height: 150px;">
                    <td  colspan="3" style="width: 1000px"></td>

                </tr>
                <tr style="height: 300px">
                    <td style="width:300px"></td>
                    <td style="width:300px;color:#2B6FB6;background-color: #CBCFD4;">
                        <center>
                            <form action="../views/Admin_login.php" method="post">

                                <div style="width: 300px;height: 70px"><h2 style="color:#6f3636">Đăng Nhập SalemsFlower </h2></div>
                                <div style="width: 300px;height: 100px">
                                    Tên Tài Khoản: <input type="text" name="login_UserName" style="margin: 0px 0px 5px 0px"><br/>
                                    Mật khẩu: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="password" name="login_Password" style="margin: 10px 0px 5px 0px"><br/>
                                    <input type="checkbox" name="login_Save" style="margin: 5px 0px 5px 0px">&nbsp;&nbsp;&nbsp;&nbsp;Nhớ tài khoản
                                </div>
<?php
if (isset($_COOKIE["UserName"]) && isset($_COOKIE["Password"])) {
    $list = $accountRes->CheckAccountAndPassword($_COOKIE["UserName"], md5($_COOKIE["Password"]));
    if ($list) {
        $_SESSION["Username"] = $_COOKIE["UserName"];
        $_SESSION["Account"] = $accountRes->GetAccountByUserName($_COOKIE["UserName"]);
        header("location:../views/admin.php");
    }
} else {
   

    if (isset($_POST["login_UserName"]) && isset($_POST["login_Password"])) {
         $list = $accountRes->CheckAccountAndPassword($_POST["login_UserName"], md5($_POST["login_Password"]));
        if ($list) {
            $_SESSION["Username"] = $_POST["login_UserName"];
            $_SESSION["Account"] = $accountRes->GetAccountByUserName($_POST["login_UserName"]);
            header("location:../views/admin.php");
        } else {
            echo "<font color='RED'> Username or Password was Wrong!!</font>";
        }
    }
}
?>
                                <div style="width: 300px;height: 50px">
                                    <input type="submit" name="submit" value="Đăng nhập"><br/><br/>
                                    <a href="../views/ChangePasword.php" style="text-decoration: none;color:#2B6FB6; ">Quên mật khẩu?</a>
                                </div>
                            </form>
                        </center>
                    </td>
                    <td style="width:300px"></td>
                </tr>
                <tr style="height: 170px">
                    <td style="width:300px"></td>
                    <td style="width:300px"></td>
                    <td style="width:300px"></td>
                </tr>
            </table>
        </center>
    </body>
</html>
<?php
ob_end_flush();
?>
